<template>
  <el-table :data="list" border fit highlight-current-row>
    <el-table-column
      v-loading="loading"
      align="center"
      width="80px"
      label="序号"
      element-loading-text="加载中"
    >
      <template slot-scope="scope">
        <span>{{ scope.row.order }}</span>
      </template>
    </el-table-column>
    <el-table-column align="center" width="140px" label="创建时间">
      <template slot-scope="scope">
        <span>{{ scope.row.createdAt | parseTime('{y}-{m}-{d}') }}</span>
      </template>
    </el-table-column>
    <el-table-column align="center" width="120px" label="名称">
      <template slot-scope="scope">
        <span>{{ scope.row.name }}</span>
      </template>
    </el-table-column>
    <el-table-column align="center" width="280px" label="操作">
      <template slot-scope="scope">
        <el-button
          size="small"
          plain
          :disabled="scope.row.order === 1"
          @click="order(scope.row.order, scope.row.order - 1)"
        >
          <i class="el-icon-arrow-up" />
        </el-button>
        <el-button
          size="small"
          plain
          :disabled="scope.row.order === list.length"
          @click="order(scope.row.order + 1, scope.row.order)"
        >
          <i class="el-icon-arrow-down" />
        </el-button>
        <el-button type="primary" plain size="small" @click="edit(scope.row)">设置</el-button>
        <el-button type="danger" plain size="small" @click="del(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import { deleteSeries, setSeriesOrder } from '../../../api/serires'
export default {
  name: 'SeriesTabPane',
  props: {
    list: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      loading: false
    }
  },
  methods: {
    switchOrder(curOrder, tarOrder) {
      const config = {}
      for (const i in this.list) {
        if (this.list[i].order === curOrder) {
          config.current = this.list[i].id
          config.curOrder = tarOrder
        } else if (this.list[i].order === tarOrder) {
          config.target = this.list[i].id
          config.tarOrder = curOrder
        }
      }
      return config
    },
    order(curOrder, tarOrder) {
      const config = this.switchOrder(curOrder, tarOrder)
      setSeriesOrder(config).then(() => {
        this.$SUCCESS('系列更新成功')
        this.$reload()
      }).catch(() => {
        this.$ERROR('系列更新失败')
      })
    },
    edit(item) {
      this.$emit('edit', item)
    },
    del(item) {
      this.$confirm('此操作将删除该章节以及该章节下的所有关卡, 是否继续?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const config = { id: item.id, order: item.order }
        deleteSeries(config).then(() => {
          this.$SUCCESS('删除系列成功')
          this.$reload()
        }).catch(() => {
          this.$ERROR('删除系列失败')
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>

</style>
